package com.clei.utils;

import com.amazonaws.ClientConfiguration;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.client.builder.AwsClientBuilder;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.amazonaws.services.s3.model.ObjectMetadata;
import com.amazonaws.services.s3.model.PutObjectRequest;

import java.io.InputStream;
import java.util.Scanner;

/**
 * amazon s3 util
 *
 * @author Y
 * @date 2024-09-10
 **/
public class S3Util {

    /**
     * endpoint
     */
    private final static String ENDPOINT;

    /**
     * bucket
     */
    private final static String BUCKET;

    /**
     * amazon s3 client
     */
    private final static AmazonS3 S3_CLIENT;

    public static void main(String[] args) {

    }

    static {
        Scanner input = new Scanner(System.in);
        PrintUtil.log("创建Amazon S3 client ");
        PrintUtil.print("Bucket : ");
        BUCKET = input.nextLine();
        PrintUtil.print("Endpoint : ");
        ENDPOINT = input.nextLine();
        PrintUtil.print("AccessKey : ");
        String accessKey = input.nextLine();
        PrintUtil.print("AccessSecret : ");
        String accessSecret = input.nextLine();
        input.close();

        S3_CLIENT = create(accessKey, accessSecret);
    }

    /**
     * put
     *
     * @param fileName 文件名
     * @param is       InputStream
     * @param length   文件大小
     * @return 上传是否成功 true/false
     */
    public static boolean put(String fileName, InputStream is, long length) {
        try (InputStream inputStream = is) {
            ObjectMetadata metadata = new ObjectMetadata();
            metadata.setContentLength(length);
            PutObjectRequest request = new PutObjectRequest(BUCKET, fileName, inputStream, metadata);
            S3_CLIENT.putObject(request);
            PrintUtil.log("文件上传成功 viewUrl : {}", String.format("%s/%s/%s", ENDPOINT, BUCKET, fileName));
            return true;
        } catch (Exception e) {
            PrintUtil.log("文件上传失败 fileName : {}", fileName, e);
        }
        return false;
    }

    /**
     * create
     *
     * @param accessKey    accessKey
     * @param accessSecret accessSecret
     * @return AmazonS3
     */
    private static AmazonS3 create(String accessKey, String accessSecret) {
        AWSCredentials awsCredentials = new BasicAWSCredentials(accessKey, accessSecret);
        ClientConfiguration clientConfiguration = new ClientConfiguration();
        // 客户端最大连接数
        clientConfiguration.setMaxConnections(8);
        return AmazonS3ClientBuilder.standard()
                .withCredentials(new AWSStaticCredentialsProvider(awsCredentials))
                .withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(S3Util.ENDPOINT, ""))
                .withClientConfiguration(clientConfiguration)
                .build();
    }
}
